<?php
 /**
  * Class 加载
  *   
  * @author wenqiang@6rooms.com
  * @version $Id: HwLoader.class.php,v 1.0 2013/12/3 12:14:48 gaowenqiang Exp $   
  * @package
  *
  */
class HwLoader
{
	private static $basePath = INCLUDE_DIR;
	private static $suffix = '.class.php';

	private static $classes = array();
	
	/**
	 * 加载类
	 *
	 * @param string $className 类名
	 * @return bool
	 */
	public static function load($className)
	{
	//	echo __LINE__ . ' load: ' . $className;
	//	echo '<br />';
		if (class_exists($className, false)) {
			return true;
		}
		
		$key  = strtolower($className);
		if (isset(self::$classes[$key])) {
			include_once self::$classes[$key];
			
			return true;
		}

		return false;
	}
	
	/**
	 * 加载类文件
	 *
	 * @param string $className 类名
	 * @param string $basePath 搜索路径
	 * @return bool
	 */
	protected static function _load($className, $basePath = null, $psr0 = false)
	{
		$basePath = $basePath ? $basePath : self::$basePath;
		$tmpName = $psr0 ? str_replace('_', DIRECTORY_SEPARATOR, $className) : $className;
		$filename = $basePath . $tmpName . self::$suffix;

		//echo __LINE__ . ' _load: ' . $className . ' filename: ' . $filename;
		//echo '<br />';
		if (file_exists($filename)) {
			$key = strtolower($className);
			self::$classes[$key] = $filename;

			return include($filename);
		}

		return false;
	}
	
	protected static function _loadByPsr0($className)
	{
		self::_load($className, null, true);
	}

	/**
	 * 自动加载方法
	 *
	 */
	public static function _autoLoad()
	{
		spl_autoload_register(array('HwLoader', '_loadByPsr0'));
		spl_autoload_register(array('HwLoader', '_load'));
	}

	/**
	 * 注册autoload方法
	 *
	 */
	public static function setup()
	{
		spl_autoload_register(array('HwLoader', 'load'));
		spl_autoload_register(array('HwLoader', '_autoLoad'));
	}
}

HwLoader::setup();